package com.sls.api.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.log4j.Logger;

/**
 * @Description:MD5 签名校验
 * @author Wangxuegang
 * @date 2012-09-03
 * 
 */
public class UtilMD5Encryption {

	private static Logger log = Logger.getLogger(UtilMD5Encryption.class);

	/**
	 * MD5加密
	 * 
	 * @param data
	 *            被加密的字符串
	 * @return
	 */
	public static String createMD5Encryption(String data) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			try {
				md.update(data.getBytes("GBK"));
			} catch (Exception e) {
				log.info(e);
			}

			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0) {
					i = i + 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
			data = buf.toString();
		} catch (NoSuchAlgorithmException e) {
		}

		return data.toLowerCase();
	}

	/**
	 * MD5加密
	 * 
	 * @param data
	 *            被加密的字符串
	 * @return
	 */
	public static String createMD5EncryptionOfUTF8(String data) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			try {
				md.update(data.getBytes("UTF-8"));
			} catch (Exception e) {
				log.info(e);
			}

			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0) {
					i = i + 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
			data = buf.toString();
		} catch (NoSuchAlgorithmException e) {
		}

		return data.toLowerCase();
	}

	/**
	 * MD5数字签名验证
	 * 
	 * @param data
	 *            被加密的数据
	 * @param sign
	 *            签名字符串
	 * @return
	 */
	public static boolean checkMD5Signature(String data, String sign) {
		boolean flag = false;
		String encryptStr = createMD5Encryption(data.trim());
		if (encryptStr.equals(sign)) {
			flag = true;
		}

		return flag;
	}

	/**
	 * MD5数字签名验证(UTF8加密)
	 * 
	 * @param data
	 * @param sign
	 * @return
	 */
	public static boolean checkMD5SignatureOfUTF8(String data, String sign) {
		boolean flag = false;
		String encryptStr = createMD5EncryptionOfUTF8(data.trim());
		if (encryptStr.equals(sign)) {
			flag = true;
		}

		return flag;
	}
	
	/**
	 * MD5加密
	 * 
	 * @param data
	 *            被加密的字符串
	 * @return
	 */
	public static String createMD5StringOfGB2312(String data) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			try {
				md.update(data.getBytes("GB2312"));
			} catch (Exception e) {
				log.info(e);
			}

			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0) {
					i = i + 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
			data = buf.toString();
		} catch (NoSuchAlgorithmException e) {
		}

		return data.toUpperCase();
	}


}
